<?php
/**
 * Door class, keep the door data
 * @package space
 * @author alcaitiff
 */
class Door  extends ExportableObject{
    
    private $id;
    protected $type,$state,$currDurability,$maxDurability,$key,$description;

    /**
     *
     * @param int $id Door id
     */
    public function __construct($id=null){
        $this->id=$id;
        if($id!==null && $id!=0){
            $this->retrieveData();
        }else{
            $this->state='clean path';
        }
    }
    /**
     * Verify clean paths (no doors)
     * @return bool
     */
    public function isClean(){
        if($this->id==null || $this->state=='clean path' || $this->state=='broken'){
            return true;
        }else{
            return false;
        }
    }
    /**
     * Verify if is locked
     * @return bool
     */
    public function isLocked(){
        if($this->state=='locked'){
            return true;
        }else{
            return false;
        }
    }

    /**
     * ID Getter
     * @return int
     */
    public function getId(){
        return $this->id;
    }

    /**
     * Verify if door is open
     * @return bool
     */
    public function isOpen(){
        if($this->state!='closed' && $this->state!='locked'){
            return true;
        }else{
            return false;
        }

    }
    /**
     * Try to unlock
     * @param Char $char
     * @return bool
     */
    public function unlock($char){
        if($this->state!='locked'){
            return false;
        }
        if($this->key==NULL){
            MessageSystem::addErrorMessage('This door can\'t be unlocked');
        }elseif($char->holdItemName($this->key)){
            $this->changeDoorState('closed');
            return true;
        }else{
            MessageSystem::addErrorMessage('You dont have the proper key on your hands');
            return false;
        }
    }
    /**
     * Try to lock
     * @param Char $char
     * @return bool
     */
    public function lock($char){
        if($this->state=='locked' || $this->state=='clean path' ||  $this->state=='broken'){
            return false;
        }
         if($this->key==NULL){
            MessageSystem::addErrorMessage('This door can\'t be locked');
        }elseif($char->holdItemName($this->key)){
            $this->changeDoorState('locked');
            return true;
        }else{
            MessageSystem::addErrorMessage('You dont have the proper key on your hands');
            return false;
        }
    }
    /**
     * Open closed doors
     * @return bool
     */
    public function open(){
        if($this->state=='closed'){
            $this->changeDoorState('open');
            return true;
        }else{
            return false;
        }
    }
    /**
     * Close open doors
     * @return bool
     */
    public function close(){
        if($this->state=='open'){
            $this->changeDoorState('closed');
            return true;
        }else{
            return false;
        }
    }
    /**
     * Change the door state
     * @param string $value New state
     */
    private function changeDoorState($value){
        $this->state=$value;
        $this->saveState();
    }
    //***********************************************************************************************************
    //					DATABASE METHODS
    //***********************************************************************************************************
    /**
     * Retrieve DB data
     */
    private function retrieveData(){
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'SELECT  `doors`.`door_type`,'.
										'`doors`.`current_durability`, '.
                                        '`doors`.`state`, '.
										'`door_types`.`description`, '.
										'`door_types`.`durability`, '.
										'`door_types`.`key` '.
								'FROM `doors`,`door_types` '.
								'WHERE   `doors`.`door` = ? AND '.
										'`doors`.`door_type` =  `door_types`.`door_type` '.
								'LIMIT 1'
								);
	    $stmt->bind_param("i", $this->id);
	    $stmt->bind_result($this->type,$this->currDurability,$this->state,$this->description,$this->maxDurability,$this->key);
	    $stmt->execute();
	    $stmt->fetch();
  		$stmt->close();
    }
    /**
     * Save current state
     */
    private function saveState() {
		$dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'UPDATE `doors` SET 	`state`=? '.
								'WHERE `door` = ? '.
								'LIMIT 1'
								);
	    $stmt->bind_param('si',$this->state,$this->id);
	    $stmt->execute();
	    $stmt->fetch();
  		$stmt->close();
    }
}